package com.jbh.wanandroidkotlin.activity

import android.content.Context
import android.graphics.Color
import android.graphics.drawable.GradientDrawable
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.recyclerview.widget.DividerItemDecoration
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.jbh.apilibrary.AppData
import com.jbh.apilibrary.bean.WxarticleListBean
import com.jbh.apilibrary.bean.WxarticleListDatasBean
import com.jbh.apilibrary.i.ApiCallBack
import com.jbh.wanandroidkotlin.R
import com.jbh.wanandroidkotlin.adapter.BaseAdapter
import kotlinx.android.synthetic.main.activity_g_z_h_w_z_list.*
import kotlinx.android.synthetic.main.activity_g_z_h_w_z_list.mRecyclerView
import kotlinx.android.synthetic.main.activity_main.*
import kotlinx.android.synthetic.main.item_gzh_layout.view.*

class GZHWZListActivity : AppCompatActivity() {

    /**默认获取页码*/
    private var page:Int = 1
    /**动态拼接接口*/
    private var url:Int = 0

    private var adapter: MyAdapter?= null

    private val list  = mutableListOf<WxarticleListDatasBean>()


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_g_z_h_w_z_list)
        initView()
    }


    private fun initView() {
        val l=LinearLayoutManager(this);
        l.orientation  = LinearLayoutManager.VERTICAL ;
        mRecyclerView.layoutManager = l ;

        val d = DividerItemDecoration(this,RecyclerView.VERTICAL)
        val g = GradientDrawable()
        g.setSize(0,5);
        g.setColor(Color.RED)
        d.setDrawable(g)
        mRecyclerView.addItemDecoration(d)

        adapter = MyAdapter(this@GZHWZListActivity, R.layout.item_gzh_layout,list)
        mRecyclerView.adapter = adapter
        url = intent.getIntExtra("urlId",0)
        wxarticleListTask(url,page)
    }


    /**
     * 获取某一个公众号历史记录
     */
    fun wxarticleListTask(id:Int, page:Int){
        AppData.instance().wxarticle_ListTask(id,page,object :
            ApiCallBack<WxarticleListBean> {
            override fun success(data: WxarticleListBean) {
                list.addAll(data.datas)
                adapter?.notifyDataSetChanged()
            }
            override fun error(error: String) {
            }
        })
    }



    class MyAdapter(context: Context,  itemView:Int, list:List<WxarticleListDatasBean>) :
        BaseAdapter<WxarticleListDatasBean>(context,itemView,list) {

        override fun onBindView(holder: RecyclerView.ViewHolder, t: WxarticleListDatasBean) {
            super.onBindView(holder, t)
         t.run {
             holder.itemView.title.text = title
         }
        }
    }
}